package com.liuheqiao.sushegaunli;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.liuheqiao.sushegaunli.dao.UserDao;
import com.liuheqiao.sushegaunli.db.DBHelper;
import com.liuheqiao.sushegaunli.model.User;

public class UserInfoActivity extends AppCompatActivity {
    private ImageView ivAvatar;
    private TextView tvUsername, tvRealName, tvRoomNo, tvRole;
    private Button btnChangePassword, btnLogout;
    private DBHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);

        initViews();
        loadUserInfo();
        setupClickListeners();
    }

    private void initViews() {
        ivAvatar = findViewById(R.id.iv_avatar);
        tvUsername = findViewById(R.id.tv_username);
        tvRealName = findViewById(R.id.tv_real_name);
        tvRoomNo = findViewById(R.id.tv_room_no);
        tvRole = findViewById(R.id.tv_role);
        btnChangePassword = findViewById(R.id.btn_change_password);
        btnLogout = findViewById(R.id.btn_logout);

        // 设置返回按钮
        findViewById(R.id.btn_back).setOnClickListener(v -> finish());
    }

    private void loadUserInfo() {
        dbHelper = new DBHelper(this);
        UserDao userDao = new UserDao(dbHelper);
        User currentUser = userDao.getCurrentUser(this);
        
        if (currentUser != null) {
            tvUsername.setText(currentUser.getUsername());
            tvRealName.setText(currentUser.getRealName());
            
            // 设置宿舍信息
            if ("-1".equals(currentUser.getBuildingNo()) || "-1".equals(currentUser.getRoomNo())) {
                tvRoomNo.setText("未分配");
            } else {
                String roomInfo = String.format("%s栋%s", 
                        currentUser.getBuildingNo(), 
                        currentUser.getRoomNo());
                tvRoomNo.setText(roomInfo);
            }
            
            // 设置角色
            tvRole.setText("admin".equals(currentUser.getRole()) ? "管理员" : "学生");
        }
    }

    private void setupClickListeners() {
        // 修改密码
        btnChangePassword.setOnClickListener(v -> {
            // TODO: 跳转到修改密码页面
            Toast.makeText(this, "修改密码功能开发中", Toast.LENGTH_SHORT).show();
        });

        // 退出登录
        btnLogout.setOnClickListener(v -> {
            // 清除登录状态
            getSharedPreferences("app_prefs", MODE_PRIVATE)
                    .edit()
                    .remove("current_username")
                    .apply();

            // 跳转到登录页面
            Intent intent = new Intent(this, LoginActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
            finish();
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dbHelper != null) {
            dbHelper.close();
        }
    }
} 